package com.lagou.http;

import com.lagou.util.HttpProtocolUtil;
import com.lagou.util.StaticResourceUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author luohq
 *
 * 写入outpustream
 */
public class Response {
    private OutputStream outputStream;

    public Response() {
    }

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public void outPut(String content) throws IOException {
        outputStream.write(content.getBytes());
    }

    public void outPutHtml(String path) throws IOException {
        String absoluteResourcePath = StaticResourceUtil.getAbsolutePath(path);
        File file = new File(absoluteResourcePath);
        if(file.exists() && file.isFile()){
            StaticResourceUtil.outPutStaticResource(new FileInputStream(file), outputStream);
        }else{
            outPut(HttpProtocolUtil.getHttpHeader400());
        }
    }
}
